package com.yimaxiaoerlang.http;

import static com.yimaxiaoerlang.http.NetType.NETWORK_MOBILE;
import static com.yimaxiaoerlang.http.NetType.NETWORK_NONE;
import static com.yimaxiaoerlang.http.NetType.NETWORK_WIFI;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetUtils {


    private static final NetUtils ourInstance = new NetUtils();

    static NetUtils getInstance() {
        return ourInstance;
    }

    private NetUtils() {

    }

    /**
     * 获取手机当前的网络状态
     * @param context
     * @return
     */
    public NetType getNetWorkState(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                return NETWORK_WIFI;
            } else if (activeNetworkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                return NETWORK_MOBILE;
            }
        } else {
            return NETWORK_NONE;
        }
        return NETWORK_NONE;
    }
}
